package com.eldos.ownerinfo;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//import android.net.Uri;
//import android.widget.TextView;

public class OwnerInfoSMSActivity extends Activity {

	public static final String SMS_EDIT = "com.eldos.ownerinfo.EditSMS"; 
	
	private String smsNumber = "";
	    
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           
        
        if (Build.VERSION.SDK_INT >= 5)
        {
        	//getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        	getWindow().addFlags(0x00080000);
        }
        
        setContentView(R.layout.ownerinfowidget_sendsms);
        
        // read the preferences
        SharedPreferences settings = null;
    	
    	// Bug alert: the call to getDefaultSharedPreferences will crash the application
    	// if preferences have not been created beforehand
    	try
    	{
    		settings = PreferenceManager.getDefaultSharedPreferences(getApplication().getApplicationContext());
    	}
    	catch (Exception x)
    	{
    		// just suppress the crash
    	}
    
    	if (settings != null)
    	{
    		smsNumber = settings.getString(OwnerInfoConst.settingSMSNumber, "");
	    }

    	Button btnSMS = (Button) findViewById(R.id.btnSMS);
        
        btnSMS.setOnClickListener(mSMSListener);        
        
    }
    
	private void sendSMS(String phoneNumber, String message)
    {        
		
		String SENT = "com.eldos.ownerinfo.SMS_SENT";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

      //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_LONG).show();
                        break;
                }
                finish();
            }
        }, new IntentFilter(SENT));
        
        SmsManager sms = SmsManager.getDefault();
        try
        {
        	sms.sendTextMessage(smsNumber, null, message, sentPI, null);
        }
        catch(Exception E)
        {
        	Toast.makeText(getBaseContext(), "Exception happened: " + E.getMessage(), 
                    Toast.LENGTH_LONG).show();            
        }
    }
	
    OnClickListener mSMSListener = new OnClickListener() {
        public void onClick(View v) {

        	EditText txtMessage = (EditText) findViewById(R.id.textSMS);
        	
        	String message = txtMessage.getText().toString();                 
        	if (message.length()>0)                
        		sendSMS(smsNumber, message);                
        	else
        		Toast.makeText(getBaseContext(), "Please enter the message", Toast.LENGTH_LONG).show();
        }
    };
	
}
